<?php

class DetailController extends Controller
{
    	public $layout = 'newlayout';
        private $_news;
        private $_project;
	public function actionIndex()
	{
            $comment_form = new Comment;
            $comments = Comment::model()->findAll();
            $this->render('index', array(
                'comment_form' => $comment_form,
                'comments'=>$comments,
                ));
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
//         protected function newComment($post)
// 	{
// 		$comment=new Comment;
// 		if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
// 		{
// 			echo CActiveForm::validate($comment);
// 			Yii::app()->end();
// 		}
// 		if(isset($_POST['Comment']))
// 		{
// 			$comment->attributes=$_POST['Comment'];
// 			if($post->addComment($comment))
// 			{
// 				if($comment->commnent_status==Comment::STATUS_PENDING)
// 					Yii::app()->user->setFlash('commentSubmitted','Thank you for your comment. Your comment will be posted once it is approved.');
// 				$this->refresh();
// 			}
// 		}
// 		return $comment;
// 	}
        public function actionView($id)
        {
                $news = $this->loadModel($id);
                $this->_news = $news;
                $comment_form = $this->newComment($news);
                $category = Category::model( )->findAllByPk($news->category_id);
                $categories = Category::model( )->findAll();
                $ads = Ads::model()->findall(); // tao ra dataProvider moi
                var_dump($ads);
                exit(0);
                $login_form=new LoginForm;
                $comments = Comment::model()->findAllByAttributes(array('news_id' => $id));
		$this->render('view', array(
	                'comment_form' => $comment_form,
	                'comments'=>$comments,
	                'news' => $news,
                        'cat' => $category,
                        'categories' => $categories,
                        'login_form' => $login_form,
                        'ads' => $ads,
                ));
        }
        
        private function newComment($news) {
        	$comment = new Comment();
        	
        	if(isset($_POST['Comment'])) {
				$comment->comment_author = $_POST['Comment']['comment_author'];
				$comment->comment_title = $_POST['Comment']['comment_title'];
				$comment->comment_content = $_POST['Comment']['comment_content'];
				$comment->comment_email = $_POST['Comment']['comment_email'];
        		$comment->news_id = $news->news_id;
        
        		$comment->save();
        	}
        		
        	return $comment;
        }
        
        public function loadModel($id){
		$model=News::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
        
        
        public function actionCreate()
	{
		$model=new Comment;
                $model->news_id = $this->_news->id;
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Comment']))
		{
			$model->attributes=$_POST['Comment'];
                        var_dump($_POST['Comment']);
			//if($model->save())
				//$this->redirect(array('detail/view','id'=>$model->news_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}
}